var express = require('express');
var router = express.Router();
var Logistics = require("../models/logistics.js");
var logcompany = require("../models/logcompany.js");


//添加用户数据
router.post('/addLogistics', function(req, res, next) {

    var param = {
        company:req.body.company,
        state:req.body.state,
        courier: req.body.courier,
        phoneNum:req.body.phoneNum,
    };

    Logistics.create( param ,function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"添加成功",
                    result: "suc"
                })
            }
        }
    })
});

router.get('/getLogistics', function(req, res, next) {
    let page = parseInt(req.param("page"));
    let pageSize = parseInt(req.param("pageSize"));
    // let sort = req.param("sort");
    let skip = (page-1)*pageSize;
    let params = {};

    if (req.param("type") != ""){
        params.orderState=req.param("type")

    }

    console.log(params)
    let model = Logistics.find(params).skip(skip).limit(pageSize);
    // goodsModel.sort({"salePrice" : sort});
    //var model = Goodslist.find();
    Logistics.count(params, (err, count) => {
        if(err){
            res.json({
                status:"1",
                msg:err.message
            })
        }else {
            model.exec(function (err1, result) {
                if (err1) {
                    res.json({
                        status: "1",
                        msg: err.message
                    })
                } else {
                    res.json({
                        status: "0",
                        msg: "",
                        result: {
                            count: count,
                            list: result
                        }
                    })
                }
            });
        }
    });
});

//修改快递员
router.post('/editLogistics', function(req, res, next) {
    var param = {
        "company" : req.body.company,
        "state" : req.body.state,
        "courier" : req.body.courier,
        "phoneNum" : req.body.phoneNum
    }
    Logistics.update({
        _id:req.body.id
    },{
        $set:param
    },function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"修改成功",
                    result: "suc"
                })
            }
        }
    });
});

//删除
router.post('/delLogistics', function(req, res, next) {
    Logistics.remove({_id:req.body.id}, function (err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"删除成功",
                    result: "suc"
                })
            }
        }
    });
});

//获取物流公司
router.get('/getlogcompany', function(req, res, next) {

    var model = logcompany.find();
    model.exec(function(err,result){
        if(err){
            res.json({
                status:"1",
                msg:err.message
            })
        }else{
            res.json({
                status:"0",
                msg:"",
                result:{
                    list:result
                }
            })
        }
    });
});


//添加物流公司
router.post('/addlogcompany', function(req, res, next) {
    var param = {
        logcompany:req.body.logcompany,
        phoneNum:req.body.phoneNum
    };
    logcompany.create( param ,function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"添加成功",
                    result: "suc"
                })
            }
        }
    })
});


//修改快递公司
router.post('/editlogcompany', function(req, res, next) {
    var param = {
        "logcompany" : req.body.logcompany,
        "phoneNum" :req.body.phoneNum
    }
    logcompany.update({
        _id:req.body.id
    },{
        $set:param
    },function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"修改成功",
                    result: "suc"
                })
            }
        }
    });
});

//删除
router.post('/dellogcompany', function(req, res, next) {
    logcompany.remove({_id:req.body.id}, function (err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"删除成功",
                    result: "suc"
                })
            }
        }
    });
});



module.exports = router;